//
//  YXYVisitorView.swift
//  SinaWeibo
//
//  Created by 袁向阳 on 16/6/26.
//  Copyright © 2016年 YXY.cn. All rights reserved.
//

import UIKit
import SnapKit

// 协议
protocol YXYVisitorViewDelegate: NSObjectProtocol {
    // 协议方法
    func clickLoginBtn()
}

class YXYVisitorView: UIView {

    // 声明代理对象
    weak var visitorViewDelegate: YXYVisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1.0)
        
        setUpUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - 供外界设置控件的方法
    func visitorViewSetUI (imgName:String? , message:String? ) {
        
        if let imageName = imgName , let text = message {
            
            rotationImage.hidden = true
            coverImage.hidden = true
            let image = UIImage(named: imageName)
            iconImgeView.image = image
            textLabel.text = text
            
        }else {
            
            // 首页添加动画
            animationCustom()
        }
        
    }
    
    
    //MARK: - 创建子控件
    private func setUpUI () {
        
        addSubview(rotationImage)
        addSubview(coverImage)
        addSubview(iconImgeView)
        addSubview(textLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        // 设置约束
        rotationImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        coverImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        iconImgeView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        textLabel.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(224)
            make.height.equalTo(36)
            make.top.equalTo(coverImage.snp_bottom)
            make.centerX.equalTo(self)
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.leading.equalTo(textLabel)
            make.top.equalTo(textLabel.snp_bottom).offset(16)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.trailing.equalTo(textLabel)
            make.top.equalTo(loginButton)
        }
        
    }
    
    // 转动图片
    private lazy var rotationImage:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return imageView
    }()
    // 遮挡图片
    private lazy var coverImage:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return imageView
    }()
    // 主图片
    private lazy var iconImgeView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return imageView
    }()
    // 文字label
    private lazy var textLabel:UILabel = {
        let label = UILabel()
        label.textColor = UIColor.grayColor()
        label.textAlignment = .Center
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14)
        return label
    }()
    // 登录按钮
    lazy var loginButton: UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.addTarget(self, action: "clickBtn", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    // 注册按钮
    lazy var registerButton: UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.addTarget(self, action: "clickBtn", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    // 动画
    private func animationCustom () {
        let basic = CABasicAnimation(keyPath: "transform.rotation")
        basic.toValue = M_PI * 2
        basic.duration = 10
        basic.repeatCount = MAXFLOAT
        basic.removedOnCompletion = false
        
        rotationImage.layer.addAnimation(basic, forKey: nil)
    }
    
    //MARK: - 注册登录按钮的点击事件
    @objc private func clickBtn () {
        visitorViewDelegate?.clickLoginBtn()
    }
}
